package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Company_info;
import model.Sys_priv;
import model.User_list;

import com.et.mvc.JsonView;
import com.wzzz.util.SysDate;

public class Company_infoController extends ApplicationController {
	public JsonView getUnits(int rows, int page) throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		long total = Company_info.count(Company_info.class, cond, args); // 查询单位总数
		List<Company_info> units = Company_info.findAll(Company_info.class,cond, args,"COMPANY_ID", rows, (page - 1) * rows); // 查询单位的一页资料

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", units);
		return new JsonView(result);
	}
	public JsonView getUnitsBySelect(int rows, int page) throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		long total = Company_info.count(Company_info.class, cond, args); // 查询单位总数
		List<Company_info> units = Company_info.findAll(Company_info.class,cond, args,"COMPANY_ID", rows, (page - 1) * rows); // 查询单位的一页资料

		return new JsonView(units);
	}
	
	public JsonView getUnit(int id) throws Exception {
		Company_info unit = Company_info.find(Company_info.class, id);
		return new JsonView(unit);
	}

	/**
	 * 查询单位，分页查询
	 * 
	 * @param rows
	 *            分页大小
	 * @param page
	 *            页号
	 * @param unit_name
	 *            查询参数-按单位名称查询
	 * @return 返回单位的分页数据资料
	 * @throws Exception
	 */
	public JsonView getUnits(int rows, int page, String unit_name)
			throws Exception {

		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		if (unit_name != null && !unit_name.equals("")) { // 单位名称参数非空时增加名称查询条件
			cond += " and unit_name like ?";
			tmpArgs.add("%" + unit_name + "%");
		}
		Object[] args = tmpArgs.toArray();

		long total = Company_info.count(Company_info.class, cond, args); // 查询单位总数
		List<Company_info> units = Company_info.findAll(Company_info.class, cond, args, "company_id", rows,
				(page - 1) * rows); // 查询单位的一页资料

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", units);
		return new JsonView(result);
	}

	/**
	 * 保存新建单位
	 */
	public JsonView save(Company_info unit) throws Exception {
		User_list ul1 = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul1.getUser_company();
		unit.setParent_id(company_id);
		unit.save();
		Integer insertC = unit.getCompany_id();
		String insertName = "admin_"+unit.getCompany_name();
		//先添加角色
		Sys_priv sp = new Sys_priv();
		sp.setCompany_id(insertC.toString());
		sp.setPriv_name(insertName);
		sp.setPriv_desc(insertName);
		sp.save();
		Integer zw = sp.getId();
		//再添加默认用户 
		User_list nul = new User_list();
		nul.setUser_name(insertName);
		nul.setUser_password("123456");//系统生成的用户默认密码都是123456,可以做个随机密码，但是要返回它，所以就不做了，哈哈
		nul.setUser_time(SysDate.getToday());
		nul.setUser_company(insertC.toString());
		nul.setUser_zw(zw.toString());
		nul.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 保存修改的单位
	 */
	public JsonView update(int id) throws Exception {
		Company_info unit = Company_info.find(Company_info.class, id);
		updateModel(unit);
		unit.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 删除指定的单位
	 */
	public JsonView destroy(int id) throws Exception {
		Company_info unit = Company_info.find(Company_info.class, id);
		unit.destroy();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
}
